<?php

namespace WebExpres\ImagesBundle\Controller;

use WebExpres\ImagesBundle\Entity\Images;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller {
    
    public function indexAction() {
       return $this->render('ImagesBundle:Default:index.html.twig');
    }

    public function showlistAction($idhotel) {
        
        $em = $this->getDoctrine()->getEntityManager();
        $consultaImages = $em->getRepository('ImagesBundle:Images')->consultaImages($idhotel);

        echo "<h2>Images</h2>";
        echo "<pre>";
        print_r($consultaImages);
        echo "</pre>";
        
        return $this->render('ImagesBundle:Default:index.html.twig', array('resultat' => $consultaImages));
      
    }

    public function llistaajaxAction(Request $request) {
     
        if($request->isXmlHttpRequest()){
            
            $aMoures = $request->request->get('rutaImagen');
            $nomHotel = $request->request->get('nombreHotel');
            $aMoures2 = $request->request->get('sizeImagen');
            
            $Tmax = ini_get('upload_max_filesize');
        
            $Tmax = str_replace("M", "", $Tmax);
        
            $Tmax = ($Tmax * 1024) * 1024;
            
            if ($aMoures2<$Tmax) {
                
                $Mimages = new Images(); 
                $Mimages->setAlt(' ');
                $Mimages->setCreatedAt(new \DateTime('now'));
                $Mimages->setUpdatedAt(new \DateTime('now'));
                $Mimages->setTitle(' ');
                $Mimages->setId_hotel($nomHotel);
                $Mimages->setName($aMoures);

                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($Mimages);
                $em->flush();
                
            }
           
            echo "La Cosa va be!!";

            die();
            
        }
        
    }
    
    /*public function uploadAction() {
        
      $uploadDir = '/uploads/';
      $targetFolder = '/uploads';

      $fileTypes = array('jpg', 'jpeg', 'gif', 'png');
      
        if (!empty($_FILES)) {
	    $tempFile   = $_FILES['Filedata']['tmp_name'];
	    $uploadDir  = __DIR__ . $uploadDir;
            $targetFile = $uploadDir . $_FILES['Filedata']['name'];

	    // Validate the filetype
	    $fileParts = pathinfo($_FILES['Filedata']['name']);
	    if (in_array(strtolower($fileParts['extension']), $fileTypes)) {

	  	  // Save the file
		  move_uploaded_file($tempFile,$targetFile);
		  		  
		  echo 1;

	    } else {

		  // The file type wasn't allowed
		  echo 'Invalid file type.';

	    }
        }
     
        return $this->render('ImagesBundle:Default:index.html.twig');
        
    }*/
    
}
